package com.globant.util.log.details;

import java.util.ArrayList;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.globant.util.log.ILogFunctions;

/**
 * Log Details implementation.
 * 
 * @author gonzalo.zarza
 * @version 20130125v1.3
 */

public class LogDetailsImpl extends JdbcDaoSupport implements ILogDetailsDAO {

	/* 
	================================================================================================
	 Methods
	================================================================================================
	*/    
    /**
     * Default constructor.
     */
    public LogDetailsImpl(){ }
    
    /* ------------------------------------------------------------------------------------------ */    
    
	@Override
	public void insertNewLog(LogDetails log) throws DataAccessException {
		String query =	"INSERT INTO log_efficiency " +
						"(log_id, name, size, date_start, general_status, efficiency) " +
						"VALUES (?, ?, ?, ?, ?, ?)";		
		this.getJdbcTemplate().update(query, new Object[] {log.getLogId(), log.getName(), 
															log.getSize(), log.getDateStart(), 
															log.getGeneralStatus(), log.getEfficiency()});
				
	}
	
	/* ------------------------------------------------------------------------------------------ */
	
	@Override
	public List<LogDetails> getLogByName(String logName) throws DataAccessException {		
		String query = "SELECT * FROM log_efficiency WHERE name = ?";
		List<?>           resultSet   = this.getJdbcTemplate().query(query, new Object[]{logName}, new LogDetailsRowMapper()); 
		List<LogDetails>  logs        = new ArrayList<LogDetails>();
		
		for (Object item : resultSet){
			logs.add((LogDetails) item);
		}
		 		
		return (logs);				
	}
	
	/* ------------------------------------------------------------------------------------------ */
    
    @Override
    public List<LogDetails> getLogById(String logId) throws DataAccessException {       
        String query = "SELECT * FROM log_efficiency WHERE log_id = ?";
        List<?>           resultSet   = this.getJdbcTemplate().query(query, new Object[]{logId}, new LogDetailsRowMapper()); 
        List<LogDetails>  logs        = new ArrayList<LogDetails>();
        
        for (Object item : resultSet){
            logs.add((LogDetails) item);
        }
                
        return (logs);              
    }
	
	/* ------------------------------------------------------------------------------------------ */

	@Override
	public List<String> getUniqueLogsId() throws DataAccessException {		
	    String query = "SELECT * FROM log_efficiency";	    
	    List<?>        resultSet   = this.getJdbcTemplate().query(query, new LogDetailsRowMapper());
	    List<String>   uniqueIds   = new ArrayList<String>();
	    
	    for (Object item : resultSet){
	        String     logId       = ((LogDetails) item).getLogId();
	        if (!uniqueIds.contains(logId)){
	            uniqueIds.add(logId);
	        }
	    }
	    
	    return(uniqueIds);	    
	}
	
	/* ------------------------------------------------------------------------------------------ */
	
	@Override
	public void updateEfficiency(String logId, float newEfficiency) throws DataAccessException {
	    String query = "UPDATE log_efficiency SET efficiency = ? WHERE log_id = ? AND general_status = '" + ILogFunctions.DETAIL_STATUS_COMPLETED + "'";
	    this.getJdbcTemplate().update(query, new Object[] {newEfficiency, logId});
	}
	
	/* ------------------------------------------------------------------------------------------ */
	
	@Override
	public void updateLogStatus(String logId, String newStatus) throws DataAccessException {
		String query = "UPDATE log_efficiency SET general_status = ? WHERE log_id = ?";
		this.getJdbcTemplate().update(query, new Object[] {newStatus, logId});
	}
	
	/* ------------------------------------------------------------------------------------------ */
	
	@Override
	public void deleteLogByName(String logName) throws DataAccessException {
		String query = "DELETE FROM log_efficiency WHERE name = ?";
		this.getJdbcTemplate().update(query, new Object[] {logName});		
	}
	
/* ------------------------------------------------------------------------------------------ */
    
    @Override
    public void deleteLogById(String logId) throws DataAccessException {
        String query = "DELETE FROM log_efficiency WHERE log_id = ?";
        this.getJdbcTemplate().update(query, new Object[] {logId});       
    }
	
}
